package com.example.castday1.test;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.castday1.bean.UserA;
import com.example.castday1.mapper.UserMapper;
import org.apache.catalina.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

/**
 * @Author : 徐闯 mpcast
 * @Date : 2021-05-23 20:05:48
 * @Description : 其他条件构造器作为参数的方法
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
public class G_OtherWrapperMethod {
    @Autowired
    private UserMapper userMapper;
    @Test
    //// 查询平均id大于200的各个年龄的用户数量和年龄
    //   select count(*),age from user group by age having avg(id)>200
    public  void testSelectMaps(){
        QueryWrapper<UserA> queryWrapper = new QueryWrapper();
        queryWrapper
                .groupBy("age")
                .having("avg(id)>{0}",1)
                .select("count(*)","age");
        List<Map<String,Object>> mapList = userMapper.selectMaps(queryWrapper);
        mapList.forEach(System.out::println);
    }

    // 查询年龄为 19的 用户数量
    @Test
    public void testSelectCount(){
        int count = userMapper.selectCount(
                new QueryWrapper<UserA>()
                        .eq("age",19)
        );
        System.out.println("查询到的数据的条数为==>"+count);
    }
    @Test
    public void testSelectOne(){
        UserA  userA = userMapper.selectOne(
                new QueryWrapper<UserA>()
                    //.gt("id",1000) // 报错  因为此时查询结果不只一条  应该返回集合
                    .eq("id",1000) // 正确 查询结果只有一条  返回的是UserA对象
        );
        System.out.println(userA);
    }
}
